<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-05-03 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

//TODO, data cache
class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}

	private function excute()
	{
		switch($this->act)
		{
			case 'cache_test':
				$this->cacheTest();
				break;
			case 'hot_list':
				$this->getHotList();
				break;
			case 'type_pic':
				$this->getTypePicList();
				break;
			case 'pic_list':
				$this->getPicList();
				break;
			case 'list':
				$this->getList();
				break;
			case 'search':
				$this->searchList();
				break;
			case 'info':
				$this->getInfo();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}

		$this->showResponse();
	}
	
	private function cacheTest()
	{
		$str = 'memcache on';
		
		cache_set('cache_test', $str);
		
		echo cache_get('cache_test');
	}

	/**
	 *
	 * 获取热门文章列表
	 *
	 *
	 */
	private function getHotList()
	{
		// 默认最多返回24条记录
		if(($this->request['limit'] + $this->request['offset']) > 24)
		{
			$this->request['offset'] = 20;
			$this->request['limit'] = 4;
		}
		
		$key = md5("article_hot_list_".$this->request['limit']."_".$this->request['offset']);
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		$result = get_hot_article_list($this->request);
		$list = $result['list'];
		
		if(!check_array($list)) $this->message('ar_01', STATUS_F);
		
		foreach($list as &$temp)
		{
			$temp['litpic'] = $temp['litpic'] ? BASE_URL.$temp['litpic'] : '';
			
			$temp['pubdate'] = date('Y-m-d H:i:s', $temp['pubdate']);
		}
				
		$this->response['status'] = STATUS_S;
		$this->response['total_page'] = ($result['total_count'] < 24 )? (string)ceil($result['total_count'] / $this->request['limit']) : '3';
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
		
		cache_set($key, $this->response, 3600);
	}

	/**
	 *
	 * 存在二级分类，返回二级分类列表
	 * 否则返回分类文章列表
	 */
	private function getList()
	{
		$this->request['type_id'] = isset($this->request['type_id']) ? (int)$this->request['type_id'] : '';
		
		if(!$this->request['type_id']) $this->message('ar_02', STATUS_E);
		
		$key = md5("article_list_".$this->request['type_id']."_".$this->request['limit']."_".$this->request['offset']);
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		$list = get_article_type_list($this->request);
		
		if(!check_array($list))
		{
			$result = get_article_list($this->request);
			$list = $result['list'];
						
			if(!check_array($list)) $this->message('ar_01', STATUS_F);
						
			foreach($list as &$temp)
			{
				$temp['litpic'] = $temp['litpic'] ? BASE_URL.$temp['litpic'] : '';
								
				$temp['pubdate'] = date('Y-m-d H:i:s', $temp['pubdate']);
			}
		}
				
		$this->response['status'] = STATUS_S;
		$this->response['total_page'] = isset($result['total_count']) ? (string)ceil($result['total_count'] / $this->request['limit']) : '1';
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
		
		cache_set($key, $this->response, 3600);
	}
	
	private function getTypePicList()
	{
		$tid = isset($this->request['type_id']) ? (int)$this->request['type_id'] : '';
		if($tid == 30) $tid = '2';
		$image_type = 'HD'; // for ipad
		
		$key = md5("type_pic_list_$tid"."_$image_type");
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		$path = ROOT_PATH.CAT_IMAGE_PATH.'/'.$image_type.'/';
		$folder_list = read_dir($path, false);
		
		$image_path = '';
		if(check_array($folder_list))
		{
			foreach($folder_list as $folder_name)
			{
				list($temp_id, $temp_name) = explode('_', $folder_name);
				if((int)$temp_id == $tid)
				{
					$image_path = $folder_name;
					break;
				}
			}
		}
		
		if(!$image_path) $this->message('ar_01', STATUS_F);
		
		$list = read_dir($path.$image_path.'/');
		
		if(!check_array($list)) $this->message('ar_01', STATUS_F);
		
		$pic_list = array();
		foreach($list as $temp)
		{
			if(count($pic_list) >= 6) break;
			
			if(stripos($temp, '.') === false) continue;
			
			list($name, $ext) = explode('.', $temp);
			
			if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif' )
			{
				$pic_list[] = BASE_URL.'/'.APP_NAME.CAT_IMAGE_PATH.urlencode($image_type).'/'.urlencode($image_path).'/'.urlencode($temp);
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($pic_list);
		$this->response['list'] = $pic_list;
		
		cache_set($key, $this->response, 86400);
	}
	
/*
	private function getTypePicList()
	{
		$this->request['type_id'] = isset($this->request['type_id']) ? (int)$this->request['type_id'] : '';
		
		$key = md5("type_pic_list_".$this->request['type_id']."_".$this->request['limit']."_".$this->request['offset']);
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		$result = get_article_with_pic_list($this->request);
		$list = $result['list'];
		
		if(!check_array($list)) $this->message('ar_01', STATUS_F);
		
		$pic_list = array();
		foreach($list as $temp)
		{
			if(count($pic_list) >= 6) break;
			
			if($temp['litpic'])
			{
				$pic_list[] = BASE_URL.$temp['litpic'];
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($pic_list);
		$this->response['list'] = $pic_list;
		
		cache_set($key, $this->response, 86400);
	}
*/
	
	private function getPicList()
	{
		$this->request['type_id'] = '';
		
		$key = md5("article_pic_list_".$this->request['limit']."_".$this->request['offset']);
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		$result = get_article_with_pic_list($this->request);
		$list = $result['list'];
		
		if(!check_array($list)) $this->message('ar_01', STATUS_F);
		
		foreach($list as &$temp)
		{
			$temp['litpic'] = $temp['litpic'] ? BASE_URL.$temp['litpic'] : '';
			
			$temp['pubdate'] = date('Y-m-d H:i:s', $temp['pubdate']);
		}
				
		$this->response['status'] = STATUS_S;
		$this->response['total_page'] = (string)ceil($result['total_count'] / $this->request['limit']);
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
		
		cache_set($key, $this->response, 3600);
	}
	
	private function searchList()
	{
		$this->request['keyword'] = isset($this->request['keyword']) ? $this->request['keyword'] : '';
		
		$list = array();
		
		$key = md5("search_article_".$this->request['keyword']."_".$this->request['limit']."_".$this->request['offset']);
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		if(strlen($this->request['keyword']) > 3)
		{
			$result = search_article_list($this->request);
			$list = $result['list'];
			
			if(!check_array($list)) $this->message('ar_01', STATUS_F);
			
			foreach($list as &$temp)
			{
				$temp['litpic'] = $temp['litpic'] ? BASE_URL.$temp['litpic'] : '';
				
				$temp['pubdate'] = date('Y-m-d H:i:s', $temp['pubdate']);
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['total_page'] = isset($result['total_count']) ? (string)ceil($result['total_count'] / $this->request['limit']) : '0';
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
		
		cache_set($key, $this->response, 60);
	}

	/**
	 *
	 * 文章详情
	 * @param $id，文章ID
	 *
	 * @return 返回
		文章ID，标题，栏目，作者，日期，点击，内容简介
	 */
	private function getInfo()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		$info = get_article_info($id);
		
		if(!check_array($info)) $this->message('ar_01', STATUS_F);
		
		$info['body'] = trim(str_replace('&nbsp;', ' ', strip_tags($info['body'], '<img>')));
		$info['body'] = str_replace('/uploads/', BASE_URL.'/uploads/', $info['body']);
		$info['litpic'] = $info['litpic'] ? BASE_URL.$info['litpic'] : '';
		$info['pubdate'] = date('Y-m-d H:i:s', $info['pubdate']);
		$info['feedback_count'] = get_feedback_count($id);
		
		//var_dump($info);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $info;
	}
}